﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor.Controls
{
    public partial class FrameList : UserControl
    {
        public class FramePickedEventArgs : EventArgs
        {
        }

        public delegate void FramePickedEventHandler(Object sender, FramePickedEventArgs e);
        public event FramePickedEventHandler FramePicked;

        private int _itemCount = 0;
        public int ItemCount { get { return _itemCount; } set { _itemCount = value; Invalidate();  } }

        private int _selectedItem = -1;
        public int SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; Invalidate(); }
        }

        public class ItemHilite
        {
            public int Index = 0;
            public Brush Color = Brushes.Gray;
        }

        public List<ItemHilite> Hilites = new List<ItemHilite>();

        public FrameList()
        {
            InitializeComponent();

            BGBrush = new SolidBrush(this.BackColor);
            ItemBorderBrush = new Pen(Color.Red);
            FontBrush = new SolidBrush(Color.Red);

            ItemCount = 10;

            Hilites.Add(new ItemHilite { Index = 3, Color = Brushes.Orange });
            Hilites.Add(new ItemHilite { Index = 5, Color = Brushes.Green });

        }

        Brush BGBrush;
        Pen ItemBorderBrush;
        Brush FontBrush;

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(BGBrush, 0, 0, Width, Height);

            foreach (var Item in Hilites)
            {
                int X = 32 * Item.Index;
                e.Graphics.FillRectangle(Item.Color, X + 2, 2, 29, Height - hScrollBar1.Height - 3);
            }

            int XPlace = 0;
            for (int I = 0; I < ItemCount; ++I)
            {
                e.Graphics.DrawRectangle(ItemBorderBrush, XPlace + 1, 1, 32 - 2, Height - hScrollBar1.Height - 2);
                e.Graphics.DrawString(I.ToString(), Font, FontBrush, new Point(XPlace + 1, 2));
                XPlace += 32;
            }

            e.Graphics.Flush();
        }

        private void FrameList_Click(object sender, EventArgs e)
        {
            if (_itemCount <= 0) return;
            var mouseEvent = e as MouseEventArgs;
            var Frame = mouseEvent.X / 32;
            if (Frame < 0) Frame = 0;
            if (Frame >= _itemCount) Frame = _itemCount - 1;
            SelectedItem = Frame;
            if (FramePicked != null) FramePicked(this, new FramePickedEventArgs());
        }
    }
}
